package com.ahstu.learning.learning;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ahstu.learning.R;
import com.ahstu.learning.SubjectActivity.CourseAdapter;
import com.ahstu.learning.bean.CourseMessage;
import com.ahstu.learning.SubjectActivity.Subject;
import com.ahstu.learning.base.BaseActivity;
import com.ahstu.learning.player.VodActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

public class Course_list extends BaseActivity {

    private ListView listView;
    private LinearLayout progressBar;
    private ArrayList<Subject> sublist=new ArrayList<>();
    Context context;
    private ImageView backButton;
//    private CourseAdapter adapter;
    private TextView course_down_list;
    private String course_type;

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.course_list);
        init();
        //动态申请文件读写权限
        int permission = ActivityCompat.checkSelfPermission(Course_list.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(
                    Course_list.this,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
            );
        }
    }

    void init() {
        course_type = getIntent().getStringExtra("subject");

        course_down_list = (TextView) findViewById(R.id.course_down_list);
        course_down_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                File huancun = new File("/sdcard/diandixuetang/" + course_type + "/");
                if (huancun.exists() && huancun.isDirectory()) {
                    if (huancun.list().length > 0) {
                        Intent intent = new Intent(Course_list.this, FileManager.class);
                        intent.putExtra("subject", course_type);
                        startActivity(intent);
                    } else
                        toast("缓存列表为空");
                } else
                    toast("缓存列表为空");
            }
        });

        backButton=(ImageView)findViewById(R.id.course_backbutton);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        context=this;
        progressBar=(LinearLayout) findViewById(R.id.progress);
        listView=(ListView)findViewById(R.id.listview);
        progressBar.setVisibility(View.VISIBLE);

        BmobQuery<CourseMessage> course_query=new BmobQuery<>();
        course_query.addWhereEqualTo("CourseType",course_type);
        course_query.findObjects(Course_list.this, new FindListener<CourseMessage>() {
            @Override
            public void onSuccess(List<CourseMessage> list) {
                progressBar.setVisibility(View.GONE);
                listView.setVisibility(View.VISIBLE);
                for (int i = 0; i < list.size(); i++) {
                    Subject subject = new Subject();
                    subject.setCoursename(list.get(i).getCoursename());
                    subject.setImageurl(list.get(i).getImage());
                    subject.setPlayId(list.get(i).getPlayId());
                    subject.setPlayUrl(list.get(i).getPlayUrl());
                    subject.setCourseinfo(list.get(i).getCourseinfo());
                    subject.setObjectid(list.get(i).getObjectId());
                    subject.setCommemt(list.get(i).getComment());
                    subject.setVedioId(list.get(i).getVedioId());
                    subject.setCourseType(list.get(i).getCourseType());
                    sublist.add(subject);
                }
                listView.setAdapter(new CourseAdapter(sublist,context));
            }

            @Override
            public void onError(int i, String s) {
                Toast.makeText(Course_list.this,"查询失败",Toast.LENGTH_SHORT).show();
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int j, long l) {

                Intent intent=new Intent(Course_list.this, VodActivity.class);
                intent.putExtra("objectid", sublist.get(j).getObjectid());
                intent.putExtra("playId", sublist.get(j).getPlayId());
                intent.putExtra("playUrl", sublist.get(j).getPlayUrl());
                intent.putExtra("course_name", sublist.get(j).getCoursename());
                intent.putExtra("course_info", sublist.get(j).getCourseinfo());
                Subject subject = new Subject();
                subject.setCourse_commemt_list(sublist.get(j).getCommemt());
                subject.setCourse_number(course_type);
                subject.setCourse_title(sublist.get(j).getCoursename());
                startActivity(intent);
            }
        });
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        //unregisterReceiver(receiver);
    }
}
